##########################################################################################

library(dplyr)
library(ggplot2)
library(data.table)
library(RColorBrewer)
library(optparse)

##########################################################################################

option_list <- list(
    make_option(c("--input_file"), type = "character") ,
    make_option(c("--gene"), type = "character") ,
    make_option(c("--type_class"), type = "character") ,
    make_option(c("--driver_gene_list"), type = "character") ,
    make_option(c("--images_path"), type = "character") ,
    make_option(c("--info_file"), type = "character")
)

if(1!=1){
    
    work_dir <- "~/20220915_gastric_multiple/dna_combinePublic/"
    input_file <- "~/20220915_gastric_multiple/dna_combinePublic/mutationTime/result/All_CCF_mutTime.addShare.tsv"
	images_path <- paste(work_dir,"/images/mutRateGene",sep="")
    gene <- "MUC6"
    type_class <- "IM"
	info_file <- "~/20220915_gastric_multiple/dna_combinePublic/baseTable/STAD_Info.addBurden.MSI_MSS.addCNVType.tsv"
    driver_gene_list <- "~/20220915_gastric_multiple/dna_combinePublic/mutsig_check/All_driver.list"

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

gene <- opt$gene
input_file <- opt$input_file
type_class <- opt$type_class
driver_gene_list <- opt$driver_gene_list
info_file <- opt$info_file
images_path <- opt$images_path

dir.create(images_path , recursive = T)

###########################################################################################

info <- data.frame(fread(info_file))
dat_input <- data.frame(fread( input_file ))
driver_list <- data.frame( fread(driver_gene_list) )

###########################################################################################

Variant_Type <- c("Missense_Mutation","Nonsense_Mutation","Frame_Shift_Ins","Frame_Shift_Del","In_Frame_Ins","In_Frame_Del","Splice_Site","Nonstop_Mutation")

###########################################################################################
## 注释肿瘤样本的类型
maf <- dat_input
maf_use <- subset( maf , Variant_Classification %in% Variant_Type )
#maf_use_GC <- subset( maf_use , Class %in% c("IGC" , "DGC") )
#maf_use_GC$Class <- "GC"

###########################################################################################

info_use <- subset( subset( info , Type != "IM + IGC + DGC" & MS_Type != "MSI" ) )
use_sample <- unique(info_use$Patient)
maf_use <- subset( maf_use , ID %in% use_sample )

gene_mut_sample <- unique(subset( maf_use , Hugo_Symbol == "MUC6" & Class == type_class & ID %in% use_sample )$ID)
gene_wild_sample <- use_sample[!(use_sample %in% gene_mut_sample)]

#maf_use <- rbind( maf_use , maf_use_GC )
maf_use$Type_Mut <- ifelse( maf_use$ID %in% gene_mut_sample , paste0( gene , "_Mut_" , type_class ) , paste0( gene , "_Wild_" , type_class ) )
info_use$Type_Mut <- ifelse( info_use$Patient %in% gene_mut_sample , paste0( gene , "_Mut_" , type_class ) , paste0( gene , "_Wild_" , type_class ) )

###########################################################################################
## 按照病理类型计算突变率
## 以人为单位
mut_rate <- maf_use %>%
group_by( Hugo_Symbol , Class , Type_Mut ) %>%
summarize( MutNum = length(unique(ID)) )
mut_rate$id <- paste0( mut_rate$Class , ":" , mut_rate$Type_Mut )

class_num <- info_use %>%
group_by( Class , Type_Mut ) %>%
summarize( SampleNum = length(unique(Patient)) )
class_num$id <- paste0( class_num$Class , ":" , class_num$Type_Mut )

mut_rate <- merge( mut_rate , class_num[,c("id" , "SampleNum")] , by = "id" )
## 分IGC、DGC和IM
mut_rate$MutRate <- mut_rate$MutNum/mut_rate$SampleNum

## 提取SMG
mut_rate <- subset(mut_rate , Hugo_Symbol %in% driver_list$Gene_Symbol )

out_name <- paste0( images_path , "/MutRate." , gene , "." , type_class , ".tsv" )
write.table( mut_rate , out_name , row.names = F , quote = F , sep = "\t" )